package com.deng.performance.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.vo.EmployeeVo;
import com.deng.performance.dao.IAdminBonus;
import com.deng.util.DateTools;

public class AdminBonusImpl extends SQLHelper implements IAdminBonus {

	public List<EmployeeVo> getAdminData(int admin_id,String stat_time) {
		try{
//			List<OrganizationVo> orgList = new OrganizationImpl().getAllOrganizationByFullName(1);
			String sql = " select a.employee_id,a.name,a.sfz,a.xztm,a.his_id,a.work_id,a.daoshixingming,"  +
					" a.xingbie,a.in_use,a.renyuanxingzhi,a.shifoufanpin,"  +
					" a.shenfen,DICT_SHENFEN.name as shenfen_str,"  +
					" a.yonggongzhidu,DICT_YONGGONGZHIDU.name as yonggongzhidu_str,"  +
					" a.xingzhengkeshi,t_sys_stat_admin_department.name as xingzhengkeshi_str,"  +
					" a.keshixingzhi,DICT_KESHIXINGZHI.name as keshixingzhi_str,"  +
					" a.gongzuojixiaohesuandanyuan," +
					"	t_sys_stat_organization_vs_dispense_organization.name as gongzuojixiaohesuandanyuan_str,"  +
					" a.jingyingjixiaohesuandanyuan," +
					"	t_sys_stat_operate_department.name as jingyingjixiaohesuandanyuan_str,"  +
					" a.zhiweibiaozhunfen,a.fengxianxishu,"  +
					" a.xingzhengzhiwu1,b.name as xingzhengzhiwu1_str,"  +
					" a.xingzhengzhiwu2,c.name as xingzhengzhiwu2_str,"  +
					" a.xingzhengjibie,DICT_XINGZHENGJIBIE.name as xingzhengjibie_str,"  +
					" a.xingzhengshizhi,DICT_XINGZHENGSHIZHI.name as xingzhengshizhi_str,"  +
					" a.chushengriqi,a.ruzhishijian,a.ruyuanshijian,"  +
					" a.gangweimingcheng,DICT_GANGWEIMINGCHENG.name as gangweimingcheng_str,"  +
					" a.gangweijibie,a.linchuangjishuma,a.teshubiaozhi,"  +
					" a.gangweileibie,DICT_GANGWEILEIBIE.name as gangweileibie_str,"  +
					" a.jishuzhicheng,DICT_JISHUZHICHENG.name as jishuzhicheng_str,"  +
					" a.xianpinzhicheng,d.name as xianpinzhicheng_str,"  +
					" a.zhichengjibie,e.name as zhichengjibie_str,"  +
					" a.zhiyeleibie,DICT_DUTY_TYPE.name as zhiyeleibie_str,"  +
					" a.zhuguankeshi,a.fenguankeshi," +
					"	a.cal_jingyingjixiao,a.pay_jingyingjixiao," +
					"	a.cal_gongzuojixiao,a.pay_gongzuojixiao," +
					"	a.valid,a.pay_value," +
					"	a.zerenjintie,a.zhiweijintie,a.yuanlingjintie" +
					"	from t_stat_employee_month as a"  +
					" left join DICT_SHENFEN"  +
					" on a.shenfen=DICT_SHENFEN.id"  +
					" left join DICT_YONGGONGZHIDU"  +
					" on a.yonggongzhidu = DICT_YONGGONGZHIDU.id"  +
					" left join t_sys_stat_admin_department"  +
					" on a.xingzhengkeshi = t_sys_stat_admin_department.id"  +
					" left join DICT_KESHIXINGZHI"  +
					" on a.keshixingzhi = DICT_KESHIXINGZHI.id"  +
					" left join t_sys_stat_organization_vs_dispense_organization"  +
					" on a.gongzuojixiaohesuandanyuan = t_sys_stat_organization_vs_dispense_organization.id"  +
					" left join t_sys_stat_operate_department"  +
					" on a.jingyingjixiaohesuandanyuan = t_sys_stat_operate_department.id"  +
					" left join DICT_XINGZHENGZHIWU as b"  +
					" on a.xingzhengzhiwu1 = b.id"  +
					" left join DICT_XINGZHENGZHIWU as c"  +
					" on a.xingzhengzhiwu2 = c.id"  +
					" left join DICT_XINGZHENGJIBIE"  +
					" on a.xingzhengjibie = DICT_XINGZHENGJIBIE.id"  +
					" left join DICT_XINGZHENGSHIZHI"  +
					" on a.xingzhengshizhi = DICT_XINGZHENGSHIZHI.id"  +
					" left join DICT_GANGWEIMINGCHENG"  +
					" on a.gangweimingcheng = DICT_GANGWEIMINGCHENG.id"  +
					" left join DICT_GANGWEILEIBIE"  +
					" on a.gangweileibie = DICT_GANGWEILEIBIE.id"  +
					" left join DICT_JISHUZHICHENG"  +
					" on a.jishuzhicheng = DICT_JISHUZHICHENG.id"  +
					" left join DICT_ZHICHENGJIBIE as d"  +
					" on a.xianpinzhicheng = d.id"  +
					" left join DICT_ZHICHENGJIBIE as e"  +
					" on a.zhichengjibie = e.id"  +
					" left join DICT_DUTY_TYPE"  +
					" on a.zhiyeleibie = DICT_DUTY_TYPE.id" +
					"	where a.xingzhengkeshi = "+admin_id+
					"	and a.stat_time = '"+stat_time+"'";
			
			Connection conn = getConnect();
			
			ResultSet rs = queryData(conn,sql);
			List<EmployeeVo> l = new ArrayList<EmployeeVo>();
			while(rs.next()){
				EmployeeVo vo = new EmployeeVo();
				vo.stat_time = stat_time;
				vo.id = rs.getInt("employee_id");
				vo.name = rs.getString("name");
				vo.xingbie = rs.getInt("xingbie");
				vo.sfz = rs.getString("sfz");
				vo.xztm = rs.getString("xztm");
				vo.his_id = rs.getString("his_id");
				vo.work_id = rs.getString("work_id");
				vo.in_use = rs.getInt("in_use");
				vo.shenfen = rs.getInt("shenfen");
				vo.shenfen_str = rs.getString("shenfen_str");
				vo.renyuanxingzhi = rs.getInt("renyuanxingzhi");
				vo.shifoufanpin = rs.getInt("shifoufanpin");
				vo.yonggongzhidu = rs.getInt("yonggongzhidu");
				vo.yonggongzhidu_str = rs.getString("yonggongzhidu_str");
				vo.daoshixingming = rs.getString("daoshixingming");
				vo.xingzhengkeshi = rs.getInt("xingzhengkeshi");
				vo.xingzhengkeshi_str = rs.getString("xingzhengkeshi_str");
				vo.keshixingzhi = rs.getInt("keshixingzhi");
				vo.keshixingzhi_str = rs.getString("keshixingzhi_str");
				vo.gongzuojixiaohesuandanyuan = rs.getInt("gongzuojixiaohesuandanyuan");
				vo.gongzuojixiaohesuandanyuan_str = rs.getString("gongzuojixiaohesuandanyuan_str");//getOrganizationName(orgList,rs.getInt("gongzuojixiaohesuandanyuan"));
				vo.jingyingjixiaohesuandanyuan = rs.getInt("jingyingjixiaohesuandanyuan");
				vo.jingyingjixiaohesuandanyuan_str = rs.getString("jingyingjixiaohesuandanyuan_str");
				if(rs.getString("zhiweibiaozhunfen") != null){
					vo.zhiweibiaozhunfen_str = rs.getBigDecimal("zhiweibiaozhunfen").toString();
				}
				if(rs.getString("fengxianxishu") != null){
					vo.fengxianxishu_str = rs.getBigDecimal("fengxianxishu").toString();
				}
				
				vo.xingzhengzhiwu1 = rs.getInt("xingzhengzhiwu1");
				vo.xingzhengzhiwu1_str = rs.getString("xingzhengzhiwu1_str");
				vo.xingzhengzhiwu2 = rs.getInt("xingzhengzhiwu2");
				vo.xingzhengzhiwu2_str = rs.getString("xingzhengzhiwu2_str");
				vo.xingzhengjibie = rs.getInt("xingzhengjibie");
				vo.xingzhengjibie_str = rs.getString("xingzhengjibie_str");
				vo.xingzhengshizhi = rs.getInt("xingzhengshizhi");
				vo.xingzhengshizhi_str = rs.getString("xingzhengshizhi_str");
				vo.chushengriqi_str = DateTools.dtsJustDate(rs.getDate("chushengriqi"));
				vo.ruzhishijian_str = DateTools.dtsJustDate(rs.getDate("ruzhishijian"));
				vo.ruyuanshijian_str = DateTools.dtsJustDate(rs.getDate("ruyuanshijian"));
				vo.gangweimingcheng = rs.getInt("gangweimingcheng");
				vo.gangweimingcheng_str = rs.getString("gangweimingcheng_str");
				vo.gangweijibie = rs.getInt("gangweijibie");
				vo.gangweileibie = rs.getInt("gangweileibie");
				vo.gangweileibie_str = rs.getString("gangweileibie_str");
				vo.linchuangjishuma = rs.getString("linchuangjishuma");
				vo.jishuzhicheng = rs.getInt("jishuzhicheng");
				vo.jishuzhicheng_str = rs.getString("jishuzhicheng_str");
				vo.xianpinzhicheng = rs.getInt("xianpinzhicheng");
				vo.xianpinzhicheng_str = rs.getString("xianpinzhicheng_str");
				vo.zhichengjibie = rs.getInt("zhichengjibie");
				vo.zhichengjibie_str = rs.getString("zhichengjibie_str");
				vo.zhiyeleibie = rs.getInt("zhiyeleibie");
				vo.zhiyeleibie_str = rs.getString("zhiyeleibie_str");
				vo.teshubiaozhi = rs.getInt("teshubiaozhi");
				vo.zhuguankeshi = rs.getString("zhuguankeshi");
				vo.fenguankeshi = rs.getString("fenguankeshi");
				
				vo.yuanlingjintie = rs.getBigDecimal("yuanlingjintie")!=null?rs.getBigDecimal("yuanlingjintie").toString():"0";
				vo.zhiweijintie = rs.getBigDecimal("zhiweijintie")!=null?rs.getBigDecimal("zhiweijintie").toString():"0";
				vo.zerenjintie = rs.getBigDecimal("zerenjintie")!=null?rs.getBigDecimal("zerenjintie").toString():"0";
				vo.pay_gongzuojixiao = rs.getBigDecimal("pay_gongzuojixiao")!=null?rs.getBigDecimal("pay_gongzuojixiao").toString():"0";
				vo.cal_gongzuojixiao = rs.getBigDecimal("cal_gongzuojixiao")!=null?rs.getBigDecimal("cal_gongzuojixiao").toString():"0";
				vo.pay_jingyingjixiao = rs.getBigDecimal("pay_jingyingjixiao")!=null?rs.getBigDecimal("pay_jingyingjixiao").toString():"0";
				vo.cal_jingyingjixiao = rs.getBigDecimal("cal_jingyingjixiao")!=null?rs.getBigDecimal("cal_jingyingjixiao").toString():"0";			
							
				vo.valid = rs.getInt("valid");
				
//				if(vo.pay_gongzuojixiao != null && vo.cal_gongzuojixiao != null){
//					vo.cz_gongzuojixiao = new BigDecimal(vo.pay_gongzuojixiao).subtract(new BigDecimal(vo.cal_gongzuojixiao)).toString();
//				}else{
//					vo.cz_gongzuojixiao = "0";
//				}
//				
//				if(vo.pay_jingyingjixiao != null && vo.cal_jingyingjixiao != null){
//					vo.cz_jingyingjixiao = new BigDecimal(vo.pay_jingyingjixiao).subtract(new BigDecimal(vo.cal_jingyingjixiao)).toString();
//				}else{
//					vo.cz_jingyingjixiao = "0";
//				}
				
				vo.pay_value = rs.getBigDecimal("pay_value")!=null?rs.getBigDecimal("pay_value").toString():"0";// new BigDecimal(vo.pay_gongzuojixiao).add(new BigDecimal(vo.pay_jingyingjixiao)).add(new BigDecimal(vo.zerenjintie)).add(new BigDecimal(vo.zhiweijintie)).add(new BigDecimal(vo.yuanlingjintie)).toString();
				vo.cal_value = new BigDecimal(vo.cal_gongzuojixiao).
							add(new BigDecimal(vo.cal_jingyingjixiao)).
							add(new BigDecimal(vo.zerenjintie)).
							add(new BigDecimal(vo.zhiweijintie)).
							add(new BigDecimal(vo.yuanlingjintie)).toString();
				l.add(vo);
			}
			
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public void updatePayGongZuoJiXiao(EmployeeVo vo) {
		try{
			Connection conn = getConnect();
			String sql = "update t_stat_employee_month set pay_gongzuojixiao = "+vo.pay_gongzuojixiao+
						"	where stat_time = '"+vo.stat_time+"' and employee_id = "+vo.id+
						"	and gongzuojixiaohesuandanyuan = "+vo.gongzuojixiaohesuandanyuan;
			updateData(conn, sql);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void updatePayJingYingJiXiao(EmployeeVo vo) {
		try{
			Connection conn = getConnect();
			String sql = "update t_stat_employee_month set pay_jingyingjixiao = "+vo.pay_jingyingjixiao+
						"	where stat_time = '"+vo.stat_time+"' and employee_id = "+vo.id+
						"	and jingyingjixiaohesuandanyuan = "+vo.jingyingjixiaohesuandanyuan;
			updateData(conn, sql);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void validData(EmployeeVo vo) {
		try{
			Connection conn = getConnect();
			String sql = "update t_stat_employee_month set valid = 1," +
					"	pay_value = pay_gongzuojixiao+pay_jingyingjixiao+yuanlingjintie+zhiweijintie+zerenjintie"+
					"	where stat_time = '"+vo.stat_time+"' and employee_id = "+vo.id;
			updateData(conn, sql);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void validData(int admin_id, String stat_time) {
		try{
			Connection conn = getConnect();
			String sql = "update t_stat_employee_month set valid = 1," +
					"	pay_value = pay_gongzuojixiao+pay_jingyingjixiao+yuanlingjintie+zhiweijintie+zerenjintie"+
						"	where valid = 0 and stat_time = '"+stat_time+"' and xingzhengkeshi = "+admin_id;
			updateData(conn, sql);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

}
